package br.com.californiahotel.server.utils;

import java.lang.reflect.Method;

import br.com.californiahotel.server.hibernate.models.Apartamento;
import br.com.californiahotel.server.hibernate.models.HibernateModel;
import br.com.californiahotel.server.hibernate.models.Locacao;
import br.com.californiahotel.server.hibernate.models.Reserva;
import br.com.californiahotel.shared.models.GwtModel;
import br.com.californiahotel.shared.models.Tipo;

public class Conversor {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public HibernateModel convertModelToHibernate(Object model, Class toClass) {
		Object toObject = null;
		try {
			toObject = toClass.newInstance();
			Class fromClass = model.getClass();
			for (Method method : fromClass.getMethods()) {
				if(method.getName().startsWith("get") && !"getClass".equals(method.getName())){
					Method toMethod = toClass.getMethod("set"+method.getName().substring(3), method.getReturnType());
					toMethod.invoke(toObject, method.invoke(model));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return (HibernateModel)toObject;
	}

	public Reserva convertReservaModelToReservaHibernate(br.com.californiahotel.shared.models.Reserva model) {
		Reserva reserva = new Reserva();
		reserva.setId(model.getId());
		reserva.setHospede(model.getHospede());
		reserva.setData(model.getData());
		return reserva;
	}

	public br.com.californiahotel.shared.models.Reserva convertReservaHibernateToReservaModel(Reserva hibernate) {
		br.com.californiahotel.shared.models.Reserva reserva = new br.com.californiahotel.shared.models.Reserva();
		reserva.setId(hibernate.getId());
		reserva.setHospede(hibernate.getHospede());
		reserva.setData(hibernate.getData());
		reserva.setApartamento(hibernate.getApartamento().getNumeroApartamento().toString());
		return reserva;
	}

	public Apartamento convertApartamentoModelToApartamentoHibernate(br.com.californiahotel.shared.models.Apartamento model) {
		Apartamento apartamento = new Apartamento();
		apartamento.setNumeroApartamento(model.getNumeroApartamento());
		return apartamento;
	}

	public br.com.californiahotel.shared.models.Apartamento convertApartamentoHibernateToApartamentoModel(Apartamento hibernate) {
		br.com.californiahotel.shared.models.Apartamento apartamento = new br.com.californiahotel.shared.models.Apartamento();
		apartamento.setNumeroApartamento(hibernate.getNumeroApartamento());
		apartamento.setTipo((Tipo) convertHibernateToModel(hibernate.getTipo(),Tipo.class));
		return apartamento;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public GwtModel convertHibernateToModel(Object model, Class toClass) {
		Object toObject = null;
		try {
			toObject = toClass.newInstance();
			Class fromClass = model.getClass();
			for (Method method : fromClass.getMethods()) {
				if(method.getName().startsWith("get") && !"getClass".equals(method.getName())){
					Method toMethod = toClass.getMethod("set"+method.getName().substring(3), method.getReturnType());
					toMethod.invoke(toObject, method.invoke(model));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return (GwtModel)toObject;
	}

	public br.com.californiahotel.shared.models.Locacao convertLocacaoHibernateToLocacaoModel(Locacao hibernate) {
		br.com.californiahotel.shared.models.Locacao locacao = new br.com.californiahotel.shared.models.Locacao();
		locacao.setApartamento(hibernate.getApartamento().getNumeroApartamento().toString());
		locacao.setHospede(hibernate.getHospede().getNome());
		locacao.setId(hibernate.getId());
		return locacao;
	}

}
